package com.coldworks.coldjoke.ui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.JokeDbManager;
import com.coldworks.coldjoke.model.JokeModel;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class JokeQuickCell extends RelativeLayout {
	private static Drawable defaultRabbit = null;
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Context context;
	private LinearLayout cell;
	private TextView jokeText;
	private ImageView jokeImgThumb;
	private TextView userName;
	private ImageView replyIcon;
	private ImageView likeIcon;
	private ImageView dislikeIcon;
	private TextView likeNum;
	private TextView dislikeNum;
	private TextView replyNum;
	private String playingId;
	private ImageView gifLogo;

	public JokeQuickCell(Context context) {
		super(context);
		this.context = context;
		this.cell = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.navigation_joke_quick_cell, this, false);
		this.jokeText = (TextView) cell.findViewById(R.id.joke_text);
		this.jokeImgThumb = (ImageView) cell.findViewById(R.id.joke_image);
		this.userName = (TextView) cell.findViewById(R.id.joke_user);
		this.replyIcon = (ImageView) cell.findViewById(R.id.reply_icon);
		this.likeIcon = (ImageView) cell.findViewById(R.id.like_icon);
		this.dislikeIcon = (ImageView) cell.findViewById(R.id.dislike_icon);
		this.replyNum = (TextView) cell.findViewById(R.id.reply_num);
		this.likeNum = (TextView) cell.findViewById(R.id.like_num);
		this.dislikeNum = (TextView) cell.findViewById(R.id.dislike_num);
		this.gifLogo = (ImageView) cell.findViewById(R.id.gif_logo);
		addView(cell);
		if (defaultRabbit == null) {
			defaultRabbit = context.getResources().getDrawable(
					R.drawable.ic_photo);
		}
	}

	@SuppressWarnings("deprecation")
	public void setData(int position, JokeModel pJoke, String jokesSort,
			boolean selected) {
		this.jokeText.setText(pJoke.getJokeText());
		this.userName.setText(pJoke.getUserName());
		this.likeNum.setText(String.valueOf(pJoke.getGoodNum()));
		this.dislikeNum.setText(String.valueOf(pJoke.getBadNum()));
		this.replyNum.setText(String.valueOf(pJoke.getReplyNum()));
		// 测试用
		// this.likeNum.setText("8888");
		// this.dislikeNum.setText("8888");
		// this.replyNum.setText("8888");

		if (pJoke.getRead()) { // 已读
			jokeText.setTextColor(context.getResources().getColor(
					R.color.text_color_3));
			// jokeImgThumb.setAlpha(0.8f);
			jokeImgThumb.setAlpha(204);
			replyIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_reply_num_0));
			likeIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_good_num_0));
			dislikeIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_bad_num_0));
			if (selected) { // 已选
				cell.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.joke_quick_bg_3));
			} else { // 未选
				cell.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.joke_quick_bg_2));
			}
		} else { // 未读
			jokeText.setTextColor(context.getResources().getColor(
					R.color.text_color_1));
			// jokeImgThumb.setAlpha(1.0f);
			jokeImgThumb.setAlpha(255);
			replyIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_reply_num_1));
			likeIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_good_num_1));
			dislikeIcon.setImageDrawable(context.getResources().getDrawable(
					R.drawable.img_bad_num_1));
			if (selected) { // 已选
				cell.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.joke_quick_bg_1));
			} else { // 未选
				cell.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.joke_quick_bg_0));
			}
		}

		String imgUrl = pJoke.getImgUrl();
		this.playingId = pJoke.getJokeId();
		if (!imgUrl.equals("")) {
			if (imgUrl.endsWith("gif")) {
				gifLogo.setVisibility(ImageView.VISIBLE);
			} else {
				gifLogo.setVisibility(ImageView.GONE);
			}
			this.jokeImgThumb.setVisibility(View.VISIBLE);
			this.jokeImgThumb.setImageDrawable(defaultRabbit);
			new JokeThumbTask(context, jokesSort, pJoke.getJokeId()).execute();
		} else {
			gifLogo.setVisibility(ImageView.GONE);
			this.jokeImgThumb.setVisibility(View.GONE);
		}
	}

	public class JokeThumbTask extends AsyncTask<Void, Void, JokeModel> {
		private Context context;
		private String jokesSort;
		private String jokeId;

		public JokeThumbTask(Context context, String jokesSort, String jokeId) {
			this.context = context;
			this.jokesSort = jokesSort;
			this.jokeId = jokeId;
		}

		@Override
		protected JokeModel doInBackground(Void... params) {
			// log.info("getting thumb for : " + jokesSort + " - " + jokeId);
			return JokeDbManager.getInstance().getOneJokeFull(context,
					jokesSort, jokeId);
		}

		@Override
		protected void onPostExecute(JokeModel jokeFull) {
			if (jokeFull.getJokeId().equals(playingId)) {
				if (jokeFull.getThumb() != null) {
					jokeImgThumb.setImageBitmap(jokeFull.getThumb());
				} else {
					jokeFull.clearThumb();
					jokeFull = null;
				}
			} else {
				return;
			}
		}
	}

}
